package io.renren.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.dao.RefundContractDao;
import io.renren.entity.*;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import io.renren.dao.RefundDao;
import io.renren.service.RefundService;


@Service("refundService")
public class RefundServiceImpl implements RefundService {
    @Autowired
    private RefundDao refundDao;
    @Autowired
    private RefundContractDao refundContractDao;

    @Override
    public RefundEntity queryObject(Integer id) {
        return refundDao.queryObject(id);
    }

    @Override
    public List<RefundEntity> queryList(Map<String, Object> map) {
        return refundDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return refundDao.queryTotal(map);
    }

    @Override
    public void save(RefundEntity refund) {
        refundDao.save(refund);
    }

    @Override
    public void update(RefundEntity refund) {
        refundDao.update(refund);
    }

    @Override
    public void delete(Integer id) {
        refundDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        refundDao.deleteBatch(ids);
    }

    @Override
    public void getInComeInfo(JSONObject jsonObject) {
        String empid = jsonObject.getString("empid");
        String costtype = jsonObject.getString("costtype");
        String costfunc = jsonObject.getString("costfunc");

    }

    @Override
    public HashMap<String, Object> getRefundInfoByDSId(String id) {
        return refundDao.getRefundInfoByDSId(id);
    }

    @Override
    public void saveRefund(JSONObject jsonObject) {
        Calendar ca = Calendar.getInstance();
        int curyear = ca.get(Calendar.YEAR);
        int curmonth = ca.get(Calendar.MONTH) + 1;
        SysUserEntity user = jsonObject.getObject("user", SysUserEntity.class);

        Map<String, Object> map = new HashMap<>();
        String idss = jsonObject.getString("ids");
        //String[] ids = idss.substring(1, idss.length() - 1).split(",");

        Object obj = jsonObject.get("rowdatas");
        JSONArray jsonArray = new JSONArray();
        if (obj != null) {
            jsonArray = (JSONArray) obj;
        }
        //String pid = jsonObject.getString("pid");
        RefundEntity refund = jsonObject.getObject("refund", RefundEntity.class);
        map.put("empid", refund.getEmpid());
        map.put("year", curyear);
        map.put("month", curmonth);
        String costtype = refund.getCostType();
        String costfunc = refund.getCostFunc();
        if ("costtype01".equals(costtype)) {//交款
            for (Object o : jsonArray) {
                refund = generRefund(o, refund, user.getUsername());
                refundDao.save(refund);
            }
        } else {
            if (!StringUtils.isBlank(costfunc)) {
                if ("costfunc01".equals(costfunc)) {//协议退款
                    RefundContractEntity entity = refundContractDao.getRefundContractByEmpid(refund.getEmpid() + "");
                    if (entity != null) {
                        List<String> typelist = new ArrayList<>();
                        if (entity.getGongsType().equals("0")) {
                            typelist.add("socialSecurity03");
                        } else if (entity.getYanglType().equals("0")) {
                            typelist.add("socialSecurity01");
                        } else if (entity.getShiyType().equals("0")) {
                            typelist.add("socialSecurity05");
                        } else if (entity.getYilType().equals("0")) {
                            typelist.add("socialSecurity04");
                        } else if (entity.getDabType().equals("0")) {
                            typelist.add("socialSecurity07");
                        } else if (entity.getGongjjType().equals("0")) {
                            typelist.add("socialSecurity08");
                        }

                        map.put("list", typelist);
                        List<SocialSecurityPayDetailEntity> list = refundDao.getInComeInfoByContract(map);
                        int getNormalRecordCount = refundDao.getNormalRecordCount(map);
                        if (getNormalRecordCount <= list.size() - jsonArray.size()) {
                            for (Object o : jsonArray) {
                                refund = generRefund(o, refund, user.getUsername());
                                refundDao.save(refund);
                            }
                        }
                    }
                } else if ("costfunc02".equals(costfunc)) {//其他退款
                    List<SocialSecurityPayDetailEntity> list = refundDao.getInComeInfo(map);
                    int getNormalRecordCount = refundDao.getNormalRecordCount(map);
                    if (getNormalRecordCount <= list.size() - jsonArray.size()) {
                        for (Object o : jsonArray) {
                            refund = generRefund(o, refund, user.getUsername());
                            refundDao.save(refund);
                        }
                    }
                }
            }
        }

    }

    @Override
    public void saveRefundContract(JSONObject jsonObject) {
        Calendar ca = Calendar.getInstance();
        SysUserEntity user = jsonObject.getObject("user", SysUserEntity.class);
        //String pid = jsonObject.getString("pid");
        RefundContractEntity refundContractEntity = jsonObject.getObject("refundContract",RefundContractEntity.class);
        refundContractEntity.setOperator(user.getUsername());
        refundContractEntity.setOperateDate(ca.getTime());
        refundContractEntity.setStatus("0");
        refundContractDao.save(refundContractEntity);

    }

    public RefundEntity generRefund(Object o, RefundEntity refund, String username) {
        Calendar ca = Calendar.getInstance();
        JSONObject json = (JSONObject) o;
        String year = json.getString("year");
        String month = json.getString("month");
        double personpayable = json.getDouble("personPayable");
        double bubaoPayablePerson = json.getDouble("bubaoPayablePerson");
        String socialSecurityType = json.getString("socialSecurityType");
        refund.setYear(Integer.valueOf(year));
        refund.setMonth(Integer.valueOf(month));
        refund.setGrje(personpayable + bubaoPayablePerson);
        refund.setInsuranceType(socialSecurityType);
        refund.setOperator(username);
        refund.setOperatorDate(ca.getTime());
        refund.setStatus("0");

        return refund;
    }

}
